var app = require("cdxzmj_app");
cc.Class({
    extends: require(app.subGameName + "_BaseForm"),

    properties: {
        mjAtlas: cc.SpriteAtlas
    },

    OnCreateInit: function () {
        //界面层级
        this.ZorderLv = 7;
        //麻将子
        this.cardItem = this.node.getChildByName("cardItem");
        //麻将池节点
        this.content = this.node.getChildByName("scrollView").getChildByName("content");
        //关闭按钮
        this.btn_close = this.node.getChildByName("cardBox").getChildByName("btn_close");
        this.AddButton(this.btn_close)

    },

    OnShow: function (allList) {
        this.content.removeAllChildren();
        if (allList.length > 0) {
            this.showGMleftCard(allList);
        } else {
            console.warn("获取剩余牌失败");
            return
        }
    },
    //显示剩余的牌
    showGMleftCard(cards) {
        this.content.removeAllChildren();
        cards.sort(function (a, b) {
            return a - b;
        });
        for (let i = 0; i <= cards.length; i++) {
            if (cards[i] > 0) {
                let cardNode = cc.instantiate(this.cardItem);
                this.ShowCardImg(cardNode, cards[i])
                this.AddButton(cardNode)
                cardNode.active = true;
                this.content.addChild(cardNode);
            }
        }
    },
    ShowCardImg(cardNode, cardID) {
        //判断
        if (!cardNode || !cardID) {
            return;
        }
        //储存牌信息
        cardNode.cardID = cardID;
        cardNode.getChildByName("id").getComponent(cc.Label).string = cardID + "";
        //精灵
        let imageName = "";
        imageName = ["1-hand-", Math.floor(cardID / 100)].join("");
        let pokerSpriteFrame = this.mjAtlas._spriteFrames[imageName]
        if (!pokerSpriteFrame) {
            this.ErrLog("精灵图加载失败:%s", cardID);
            return
        }
        cardNode.getComponent(cc.Sprite).spriteFrame = pokerSpriteFrame;
    },
    //添加按钮缩放点击效果
    AddButton: function (buttonNode) {
        // 添加 cc.Button 组件
        let button = buttonNode.addComponent(cc.Button);
        // 设置按钮的点击缩放效果
        button.transition = cc.Button.Transition.SCALE;
        button.zoomScale = 0.95;
        button.duration = 0.1;
        // 添加事件
        buttonNode.on(cc.Node.EventType.TOUCH_END, this.OnClick, this);
    },
    OnClick(event) {
        let btnName = event.target.name
        switch (btnName) {
            case "btn_close":
                this.CloseForm()
                break;
            default:
                this.OnCardClick(event)
                break;
        }
    },
    OnCardClick(event) {
        let node = event.target
        let cardID = node.cardID
        let sendPack = {
            'handCard': cardID,
        }
        app[app.subGameName + "_NetManager"]().SendPack("game.MJSetWhiteHandCard", sendPack)
        this.CloseForm()
    }
});
